package com.ymatou.ymtadlog.testcase;

import java.awt.print.Printable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;


import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.tool.FormatUtil;
import com.ymatou.ymtadlog.parameter.ServiceLogBean;
import com.ymatou.ymtadlog.service.ServiceLogCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P2;
import com.ymttest.database.sqlwapper.YmtAdLogWapper;

public class Ts_ServiceLog {
	private static ServiceLogBean serviceLogBean;
	private static ServiceLogCall serviceLogCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("发送通知接口");
	}

	@Before
	public void caseUp() {
		serviceLogBean=new ServiceLogBean();
		serviceLogCall=new ServiceLogCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(serviceLogCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_ServiceLog_001() {
		
		try {
			Logger.start(true,"添加日志进数据库");
			
			String logid = serviceLogCall.getStringRandom(28);
			//获取请求前总记录数
			int logCount = new YmtAdLogWapper().selectLogCount();
			
			serviceLogBean.setAction("click");
			serviceLogBean.setAd_id("1");
			serviceLogBean.setAd_type(1);
			serviceLogBean.setAppversion("3.5.0");
			serviceLogBean.setCookieid("00000000-112c-4089-638d-6f800033c587");
			serviceLogBean.setIdfa("961A643C-6F59-4137-AE84-755C4F09C294");
			//serviceLogBean.setImei("99000620279561");
			serviceLogBean.setIp("172.16.27.86");
			serviceLogBean.setLogid(logid);
			serviceLogBean.setModule_index(1);
			serviceLogBean.setModule_name("search_bar");
			serviceLogBean.setOrigin("front_native");
			serviceLogBean.setOsid(1);
			serviceLogBean.setPage_type("app_home_page");
			serviceLogBean.setPageviewid("5B9FFEF3-3429-4388-B36F-AF18FFA9DC0B");
			serviceLogBean.setSproductid("45cdadd7-3fe7-46a7-87c5-6c03d94b4d87");
			serviceLogBean.setSub_module_name("product");
			serviceLogBean.setTime("1492674805387");
			serviceLogBean.setUserid(8532267);
			serviceLogBean.setYid("46fa2f3a-2cbd-4365-92ab-3838d6d2b00a");
			serviceLogBean.setSpecificationId("1");
			serviceLogBean.setDeviceid("idfa");
			
			
			List<ServiceLogBean> list=new ArrayList<ServiceLogBean>();
			list.add(serviceLogBean);
			String date=FormatUtil.beanToGSONString(list);
			Logger.debug(date);
			serviceLogCall.setData(date);
			serviceLogCall.callService();
			
			//请求后数据库中记录数
			int logCount2 = new YmtAdLogWapper().selectLogCount();
			
			Logger.comment("验证数据是否进入数据库");
			Logger.verifyEquals(logCount+1, logCount2, "日志数据是否有进入数据库");
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	@Test
	@Category(P2.class)
	@TestCase
public void Tc_ServiceLog_002() {
		
		try {
			Logger.start(true,"测试环境10秒内不会重复添加日志");
			//随机生成一个logid
			String logid = "53fdcc125a8f93684e000a875";
			//获取请求前总记录数
			int logCount = new YmtAdLogWapper().selectLogCount();
			
			serviceLogBean.setAction("click");
			serviceLogBean.setAd_id("1");
			serviceLogBean.setAd_type(1);
			serviceLogBean.setAppversion("3.5.0");
			serviceLogBean.setCookieid("00000000-112c-4089-638d-6f800033c587");
			serviceLogBean.setIdfa("961A643C-6F59-4137-AE84-755C4F09C294");
			serviceLogBean.setImei("99000620279561");
			serviceLogBean.setIp("172.16.27.86");
			serviceLogBean.setLogid(logid);
			serviceLogBean.setModule_index(1);
			serviceLogBean.setModule_name("search_bar");
			serviceLogBean.setOrigin("front_native");
			serviceLogBean.setOsid(1);
			serviceLogBean.setPage_type("app_home_page");
			serviceLogBean.setPageviewid("5B9FFEF3-3429-4388-B36F-AF18FFA9DC0B");
			serviceLogBean.setSproductid("45cdadd7-3fe7-46a7-87c5-6c03d94b4d87");
			serviceLogBean.setSub_module_name("product");
			serviceLogBean.setTime("1492674805387");
			serviceLogBean.setUserid(8532267);
			serviceLogBean.setYid("46fa2f3a-2cbd-4365-92ab-3838d6d2b00a");
			serviceLogBean.setSpecificationId("1");
			serviceLogBean.setDeviceid("idfa");
			List<ServiceLogBean> list=new ArrayList<ServiceLogBean>();
			list.add(serviceLogBean);
			String date=FormatUtil.beanToGSONString(list);
			Logger.debug(date);
			serviceLogCall.setData(date);
			serviceLogCall.callService();
			
			//请求后数据库中记录数
			int logCount2 = new YmtAdLogWapper().selectLogCount();
			serviceLogCall.callService();
			int logCount3 = new YmtAdLogWapper().selectLogCount();
			
			Logger.comment("验证相同logid的数据不会进入数据库");
			Logger.verifyEquals(logCount2, logCount3, "测试环境10秒内相同logid的日志数据不会进入数据库");
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_ServiceLog_003() {

		try {
			Logger.start(true, "只传logid日志数据能够保存成功");
			// 随机生成一个logid
			String logid = serviceLogCall.getStringRandom(28);
			// 获取请求前总记录数
			int logCount = new YmtAdLogWapper().selectLogCount();

			serviceLogBean.setLogid(logid);

			List<ServiceLogBean> list = new ArrayList<ServiceLogBean>();
			list.add(serviceLogBean);
			String date = FormatUtil.beanToGSONString(list);
			
			serviceLogCall.setData(date);
			serviceLogCall.callService();

			// 请求后数据库中记录数
			int logCount2 = new YmtAdLogWapper().selectLogCount();

			Logger.comment("验证相同logid的数据不会进入数据库");
			Logger.verifyEquals(logCount + 1, logCount2,
					"除logid外其他参数不传也能保存进入数据库");

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_ServiceLog_004() {

		try {
			Logger.start(true, "验证所有插入字段");
			// 随机生成一个logid
			String logid = serviceLogCall.getStringRandom(28);
			String actionsString = "click";
			String adid = "1";
			int adtype = 1;
			String appverstionString = "3.5.0";
			String cookieid = "00000000-112c-4089-638d-6f800033c587";
			String idfa = "961A643C-6F59-4137-AE84-755C4F09C294";
			String imei = "99000620279561";
			String ipString = "172.16.13.16";
			int module_index = 1;
			String modulename = "search_bar";
			String origin = "front_native";
			int osid = 1;
			String pagetypeString = "app_home_page";
			String pageviewid = "5B9FFEF3-3429-4388-B36F-AF18FFA9DC0B";
			String sproductid = "45cdadd7-3fe7-46a7-87c5-6c03d94b4d87";
			String submodulenameString = "product";
			String timeString = "1492674805387";
			int userid = 8532267;
			String yidString = "46fa2f3a-2cbd-4365-92ab-3838d6d2b00a";
			String speciString = "1";
			String deviceid = "idfa";
			
			// 获取请求前总记录数
			int logCount = new YmtAdLogWapper().selectLogCount();

			serviceLogBean.setAction(actionsString);
			serviceLogBean.setAd_id(adid);
			serviceLogBean.setAd_type(adtype);
			serviceLogBean.setAppversion(appverstionString);
			serviceLogBean.setCookieid(cookieid);
		//	serviceLogBean.setIdfa(idfa);
			//serviceLogBean.setImei(imei);
			serviceLogBean.setIp(ipString);
			serviceLogBean.setLogid(logid);
			serviceLogBean.setModule_index(module_index);
			serviceLogBean.setModule_name(modulename);
			serviceLogBean.setOrigin(origin);
			serviceLogBean.setOsid(osid);
			serviceLogBean.setPage_type(pagetypeString);
			serviceLogBean.setPageviewid(pageviewid);
			serviceLogBean.setSproductid(sproductid);
			serviceLogBean.setSub_module_name(submodulenameString);
			serviceLogBean.setTime(timeString);
			serviceLogBean.setUserid(userid);
			serviceLogBean.setYid(yidString);
			serviceLogBean.setSpecificationId(speciString);
			serviceLogBean.setDeviceid(deviceid);
			
			List<ServiceLogBean> list = new ArrayList<ServiceLogBean>();
			list.add(serviceLogBean);
			String date = FormatUtil.beanToGSONString(list);
			
			serviceLogCall.setData(date);
			serviceLogCall.callService();
			
			//获取数据库中插入记录
			
			List<Map> logDetails = new YmtAdLogWapper().selectLogDetails();
			
			Logger.verifyEquals(actionsString,logDetails.get(0).get("action_type"),"检查action值");
			Logger.verifyEquals(adid,logDetails.get(0).get("ad_id") ,"检查ad_id值");
			Logger.verifyEquals(adtype,logDetails.get(0).get("ad_type") ,"检查ad_type值");
			Logger.verifyEquals(appverstionString, logDetails.get(0).get("appversion") ,"检查appversion值");
			Logger.verifyEquals(cookieid, logDetails.get(0).get("cookieid"),"检查cookieid值");
			
			Logger.verifyEquals(yidString, logDetails.get(0).get("yid"),"检查yid值");
			Logger.verifyEquals(deviceid,logDetails.get(0).get("deviceid") ,"检查deviceid值");
			Logger.verifyEquals(osid, logDetails.get(0).get("osid"),"检查osid值");
			
			Logger.verifyEquals(pagetypeString,logDetails.get(0).get("page_type") ,"检查page_type值");
			Logger.verifyEquals(pageviewid,logDetails.get(0).get("pageviewid") ,"检查pageviewid值");
			
			Logger.verifyEquals(modulename,logDetails.get(0).get("module_name") ,"检查module_name值");
			Logger.verifyEquals(origin, logDetails.get(0).get("origin") ,"检查origin值");
			Logger.verifyEquals(submodulenameString,logDetails.get(0).get("sub_module_name") ,"检查sub_module_name值");
			Logger.verifyEquals(module_index, logDetails.get(0).get("module_index"),"检查module_index值");
			Logger.verifyEquals(sproductid, logDetails.get(0).get("sproductid"),"检查sproductid值");
			Logger.verifyEquals(speciString,logDetails.get(0).get("specificationId") ,"检查specificationId值");
			

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	
}
